package com.zhongyouyun.utils;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.logging.Logger;

public class RequestModelUtil {

    private  static Logger logger = Logger.getLogger ( RequestModelUtil.class.getName () );


    /**
     * @param request
     * @param beanClass
     * @Description 接收request全部参数转换实体累
     * @Throws
     * @Return T
     * @Date 2021-01-11 10:40:40
     * @Author WangKun
     **/
    public static <T> T getRequestConvertBean(HttpServletRequest request, Class<T> beanClass) {
        try {
            // 反射实体类
            T bean = beanClass.newInstance();
            // 获取全部参数
            Map<String, String[]> map = request.getParameterMap();
            // 制定日期转换器
            ConvertUtils.register(new Converter() {
                @Override
                public Object convert(Class clazz, Object value) {
                    // 将string转成date
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                    Date parse = null;
                    try {
                        if (StringUtils.isNotBlank(value.toString())) {
                            parse = format.parse(value.toString());
                        }
                    } catch (ParseException e) {
                       // Log4j.error("日期转换失败");
                        e.printStackTrace();
                    }
                    return parse;
                }
            }, Date.class);
            // 转换
            BeanUtils.populate(bean, map);
            return bean;
        } catch (Exception e) {
            logger.warning("request 实体转换失败");
            e.printStackTrace();
        }
        return null;
    }
    /**
     * @param request
     * @param beanClass 实体
     * @Description request转换实体(该实体类中不允许有方法)
     * @Throws
     * @Return object
     * @Date 2020-10-28 11:00:33
     * @Author WangKun
     **/
    public static Object getRequestConvertBean2(HttpServletRequest request, Class<?> beanClass) {
        Object obj = null;
        try {
            obj = beanClass.newInstance(); //反射
            Field[] fields = beanClass.getDeclaredFields(); // 获取实体类字段
            Class<?> fieldType; // 字段声明类型
            String fieldName; // 字段名称
            for (Field field : fields) {
                field.setAccessible(true); // 设置该字段private可访问
                fieldName = field.getName();
                // 判断实体中 final
                if (Modifier.isFinal(field.getModifiers())){
                    continue;
                }
                // 如果为空则跳过该字段
                if (StringUtils.isBlank(request.getParameter(fieldName)) || StringUtils.isEmpty(request.getParameter(fieldName))) {
                    field.set(obj, null);
                    continue;
                }
                fieldType = field.getType(); // 字段类型声明
                // 字段类型转换
                if (fieldType == int.class || fieldType == Integer.class) {
                    field.set(obj, Integer.valueOf(request.getParameter(fieldName)));
                } else if (fieldType == long.class || fieldType == Long.class) {
                    field.set(obj, Long.valueOf(request.getParameter(fieldName)));
                } else if (fieldType == float.class || fieldType == Float.class) {
                    field.set(obj, Float.valueOf(request.getParameter(fieldName)));
                } else if (fieldType == double.class || fieldType == Double.class) {
                    field.set(obj, Double.valueOf(request.getParameter(fieldName)));
                } else if (fieldType == boolean.class || fieldType == Boolean.class) {
                    field.set(obj, Boolean.valueOf(request.getParameter(fieldName)));
                } else if (fieldType == short.class || fieldType == Short.class) {
                    field.set(obj, Short.valueOf(request.getParameter(fieldName)));
                } else if (fieldType == BigDecimal.class) {
                    field.set(obj, BigDecimal.valueOf(Long.parseLong(request.getParameter(fieldName))));
                } else if (fieldType == Date.class) {
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                    field.set(obj, formatter.parse(request.getParameter(fieldName)));
                } else if (fieldType == String.class) {
                    field.set(obj, request.getParameter(fieldName));
                } else {
                    logger.warning("字段类型未知");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }
}
